package cn.kgm.util;

import java.util.ArrayList;
import java.util.List;

public class ParameterMappingTokenHandler implements TokenHandler {

    private List<ParameterMapping> parameterMappings = new ArrayList<ParameterMapping>();

    public ParameterMappingTokenHandler() {
    }

    public List<ParameterMapping> getParameterMappings() {
        return parameterMappings;
    }

    public String handleToken(String content) {
        parameterMappings.add(buildParameterMapping(content));
        return "?";
    }

    private ParameterMapping buildParameterMapping(String content) {
        ParameterMapping parameterMapping = parseParameterMapping(content);
        return parameterMapping;
    }

    private ParameterMapping parseParameterMapping(String content) {
        try {
            return new ParameterMapping(content);
        } catch (Exception ex) {
            throw new RuntimeException("Parsing error was found in mapping @{" + content + "}.  Check syntax #{property|(expression), var1=value1, var2=value2, ...} ", ex);
        }
    }
}
